package com.hshx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hshx.VO.JobpostingVo;
import com.hshx.client.DeptClient;
import com.hshx.client.Job_classificationClient;
import com.hshx.client.PositionClient;
import com.hshx.pojo.Dept;
import com.hshx.pojo.Job_classification;
import com.hshx.pojo.Job_posting;
import com.hshx.pojo.Position;
import com.hshx.service.Job_postingService;
import com.hshx.util.RedisUtil1;
import com.hshx.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/zhaopingService")
public class Job_postingController {


    @Resource
    private Job_postingService jobService;
    @Resource
    private DeptClient deptClient;
    @Resource
    private PositionClient positionClient;
    @Resource
    private Job_classificationClient jobClassificationClient;
    @Autowired
    private RedisUtil1 redisUtil1;


    @RequestMapping(value = "/getAllJob")
    public List<JobpostingVo> getAllJob(int indexPage, int pageSize) throws ParseException {

        List<JobpostingVo> jobpostingVos = null;

        if (null != getRedis(indexPage,pageSize)){
            jobpostingVos = getRedis(indexPage,pageSize);
        }else{
            //1、通过Redis获取，如果Redis不存在，那么查询数据库，存入Redis中
            jobpostingVos = new ArrayList<>();
            List<Job_posting> jobPostings = jobService.list(new QueryWrapper<Job_posting>()
                    .orderByDesc("job_posting_id"));

            for (Job_posting job:jobPostings) {
                JobpostingVo jobVo = new JobpostingVo();
                jobVo = setJobVo(jobVo,job);
                jobpostingVos.add(jobVo);
            }

            setRedis(jobpostingVos);    // 保存数据到Redis
        }

        return jobpostingVos;
    }

    @RequestMapping(value = "/addJobposting")
    public boolean addJobposting(Job_posting jobPosting,String startDate,String endDate) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(startDate);
        Date date2= simpleDateFormat.parse(endDate);
        jobPosting.setJob_posting_startDate(date);
        jobPosting.setJob_posting_endDate(date2);

        boolean res = jobService.save(jobPosting);
        List<JobpostingVo> jobpostingVos = null;
        JobpostingVo jobVo = new JobpostingVo();

        if (res){
            if (null != getRedis(0,-1)){
                jobpostingVos =  getRedis(0,-1);
            }

            jobVo = setJobVo(jobVo,jobPosting);
            jobpostingVos.add(jobVo);

            removeRedis();
            setRedis(jobpostingVos);    // 保存数据到Redis
        }
        return res;
    }

    @RequestMapping(value = "/getOneJob")
    public Job_posting getOneJob(int job_posting_id){
//        Job_posting job = jobService.getById(job_posting_id);
        return jobService.getById(job_posting_id);
    }

    @RequestMapping(value = "/updJobById")
    public boolean updJobById(Job_posting job_posting){
        System.out.println(job_posting);
        boolean res = jobService.updateById(job_posting);
        removeRedis();
        try {
            getAllJob(0,5);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return res;
    }


//    @HystrixCommand(fallbackMethod = "fallback")
    @RequestMapping(value = "/delJobById")
    public boolean delJobById(int jobId){
        if (null != getRedis(0,5)){
            List<JobpostingVo> jobpostingVos = getRedis(0,5);

            JobpostingVo jobpostingVo = new JobpostingVo();
            for (JobpostingVo jobs:jobpostingVos) {
                if (jobs.getJob_posting().getJob_posting_id()==jobId){
                    jobpostingVo = jobs;
                    break;
                }
            }
            jobpostingVos.remove(jobpostingVo);
            jobpostingVos.forEach(System.out::println);
            removeRedis();
            setRedis(jobpostingVos);
        }
        return jobService.removeById(jobId);
    }

    public void removeRedis(){
        System.out.println("removeRedis()");
        redisUtil1.remove("jobpostingVo_List");
    }

    public void setRedis(List<JobpostingVo> jobpostingVos){
        System.out.println("setRedis()");
        redisUtil1.lPushAll("jobpostingVo_List",jobpostingVos,60L*24);
    }

    public List<JobpostingVo> getRedis(int indexPage,int pageSize){
        System.out.println("getRedis()");
        List<JobpostingVo> jobpostingVos = null;
        List<Object> objects=redisUtil1.lRange("jobpostingVo_List", indexPage, pageSize);
        if(objects!=null&&objects.size()>0){
            jobpostingVos = objects.stream().map(j->(JobpostingVo)j).collect(Collectors.toList());
            removeRedis();
            setRedis(jobpostingVos);
            return jobpostingVos;
        }

        return null;
    }

    public JobpostingVo setJobVo(JobpostingVo jobVo,Job_posting job) throws ParseException {
        Dept dept = deptClient.getDeptOne(job.getJob_posting_dept());
        Position position = positionClient.getPosition(job.getJob_posting_position());

        Job_classification jobClassification = jobClassificationClient.getJob_classification(position.getPosition_job());
            /*
                招聘表两个日期字段的转换
            * */
        java.sql.Date resultDate1 = Util.timeConvert(job.getJob_posting_startDate());
        java.sql.Date resultDate2 = Util.timeConvert(job.getJob_posting_endDate());

        job.setJob_posting_startDate(resultDate1);
        job.setJob_posting_endDate(resultDate2);

        jobVo.setJob_posting(job);
        jobVo.setDept(dept);
        jobVo.setPosition(position);
        jobVo.setJob_classification(jobClassification);

        return jobVo;
    }

}
